// QPContrlPanel.cpp : implementation file
//

#include "stdafx.h"
#include "QuadIntrinsic.h"
#include "QPContrlPanel.h"
#include "QuadPaint.h"
#include <conio.h>
#include "PaintWnd.h"


// CQPContrlPanel dialog

IMPLEMENT_DYNAMIC(CQPContrlPanel, CDialog)

CQPContrlPanel::CQPContrlPanel(CWnd* pParent /*=NULL*/)
	: CDialog(CQPContrlPanel::IDD, pParent)
	
{
	if (typeid(*pParent) == typeid(CQuadPaint))
	{
		m_pParent = (CQuadPaint *)pParent;
	}
	else
	{
		AfxMessageBox(_T("CQPContrlPanel input parent type error."), MB_OK);
	}
	m_pPaintWnd = NULL;
	
}

CQPContrlPanel::~CQPContrlPanel()
{
}

void CQPContrlPanel::setPaintWnd(CPaintWnd  * pWnd)
{
	assert(pWnd);
	m_pPaintWnd = pWnd;
}

bool CQPContrlPanel::isLocalBrush()
{
	return m_buttonLocalBrush.GetCheck() == BST_CHECKED;
}
void CQPContrlPanel::EnableRefineButton()
{
	m_buttonLocalBrush.SetCheck(BST_UNCHECKED);
	m_refineBrush.SetCheck(BST_CHECKED);
	InvalidateRect(0, FALSE);
}

void CQPContrlPanel::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RAD_LOCAL_BRUSH, m_buttonLocalBrush);
	DDX_Control(pDX, IDC_RAD_LOCAL_REFINE, m_refineBrush);
	DDX_Control(pDX, IDC_COM_SHOW_RESULT_TYPE, m_showResultType);
}


BEGIN_MESSAGE_MAP(CQPContrlPanel, CDialog)
	ON_BN_CLICKED(IDC_RAD_LOCAL_BRUSH, &CQPContrlPanel::OnBnClickedRadLocalBrush)
	ON_BN_CLICKED(IDC_BUT_CP_UNDO, &CQPContrlPanel::OnBnClickedButCpUndo)
	ON_BN_CLICKED(IDC_BUT_SAVE, &CQPContrlPanel::OnBnClickedButSave)
	ON_BN_CLICKED(IDC_BUT_QUAD_SOLVER, &CQPContrlPanel::OnBnClickedButQuadSolver)
	ON_BN_CLICKED(IDC_RAD_LOCAL_REFINE, &CQPContrlPanel::OnBnClickedRadLocalRefine)
	ON_CBN_SELCHANGE(IDC_COM_SHOW_RESULT_TYPE, &CQPContrlPanel::OnCbnSelchangeComShowResultType)
END_MESSAGE_MAP()


// CQPContrlPanel message handlers

BOOL CQPContrlPanel::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	MoveWindow(10, 600, 450, 350);
	m_buttonLocalBrush.SetCheck(BST_CHECKED);
	
	//set show result type
	m_showResultType.SetItemData(m_showResultType.AddString("0 Org Img"), PW_SHOW_ORG);
	m_showResultType.SetItemData(m_showResultType.AddString("1 Albedo"), PW_SHOW_ALBEDO);
	m_showResultType.SetItemData(m_showResultType.AddString("2 Shading"), PW_SHOW_ORG);
	m_showResultType.SetCurSel(0);
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

//BOOL CQPContrlPanel::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
//{
//	// TODO: Add your specialized code here and/or call the base class
//
//	return CDialog::OnNotify(wParam, lParam, pResult);
//}

void CQPContrlPanel::PostNcDestroy()
{
	// TODO: Add your specialized code here and/or call the base class
	CDialog::PostNcDestroy();

	if(m_pParent)
	{		
		m_pParent->m_controlPanel = NULL;		
	}
	delete this;
}

void CQPContrlPanel::OnOK()
{
	// TODO: Add your specialized code here and/or call the base class
	if (UpdateData(true))
	{
		m_pParent->UpdateData(false);
		DestroyWindow();
	}

//	CDialog::OnOK();
}

void CQPContrlPanel::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class
	DestroyWindow();
//	CDialog::OnCancel();
}

void CQPContrlPanel::OnBnClickedRadLocalBrush()
{
	// TODO: Add your control notification handler code here
	if (m_buttonLocalBrush.GetCheck() == BST_CHECKED)
	{
		cprintf("local brush checked.\n");
		m_pPaintWnd->EnableLocalBrush();
	}
	
}

void CQPContrlPanel::OnBnClickedButCpUndo()
{
	// TODO: Add your control notification handler code here
	if (m_pPaintWnd)
	{
		m_pPaintWnd->Undo();
	}
}

void CQPContrlPanel::OnBnClickedButSave()
{
	// TODO: Add your control notification handler code here
	if (m_pPaintWnd)
	{
		m_pPaintWnd->Save();
	}
}

void CQPContrlPanel::OnBnClickedButQuadSolver()
{
	// TODO: Add your control notification handler code here
	if (m_pPaintWnd)
	{
		m_pPaintWnd->QuadSolve();
	}

}

void CQPContrlPanel::OnBnClickedRadLocalRefine()
{
	// TODO: Add your control notification handler code here
	if (m_refineBrush.GetCheck() == BST_CHECKED)
	{
		cprintf("refine brush checked.\n");
		m_pPaintWnd->EnableRefineBrush();
	}

}

void CQPContrlPanel::OnCbnSelchangeComShowResultType()
{
	// TODO: Add your control notification handler code here
	int showType = m_showResultType.GetItemData(m_showResultType.GetCurSel());
	if (m_pPaintWnd)
	{
		m_pPaintWnd->SetShowType(showType);
	}
	
}
